// pages/queueNumber/index.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    animationData:{},
    showModalStatus:false,
    lineIist:[],
    storeInfo:{},
    locationAdress:{},
    showInline:true,
    userNumberInfo:{},
    animationDatalocal:{},
    showAnimation:true
  },
  
  //刷新排队进度
  renQueueNumber:function(){
    this.checkInline();
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getStoreInfo();
    this.getLocationInfo();
  },

  //判断当前用户是否在排队
  checkInline:function(){
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    wx.request({
      url: app.globalData.host + "/api/Restaurant/CheckInlineQueue?storeId=" + app.globalData.storeId + "&uid=" + app.globalData.userInfo.Uid,
      success: function(res) {
        wx.hideLoading();
        if(res.data.Code == 0){
          that.setData({
            showInline: res.data.Data
          })
          if(res.data.Data){
            that.getNumberInfo();//排队进度
          }else{
            that.getStoreLineIist();//排队队列
          }
        }
      },
    })
  },

  //获取店铺排队队列
  getStoreLineIist:function(){
    wx.showLoading({
      title: '加载中',
    })
    var that = this;
    wx.request({
      url: app.globalData.host + "/api/Restaurant/GetStoreLineItems?storeId=" + app.globalData.storeId,
      success: function(res) {
        wx.hideLoading();
        if(res.data.Code == 0){
            that.setData({
              lineIist: res.data.Data
            })
        }
      },
    })
  },
  
  //获取用户正在排队信息
  getNumberInfo:function(){
    var that = this;
    wx.request({
      url: app.globalData.host + "/api/Restaurant/GetNumber?storeId=" + app.globalData.storeId + "&uid=" + app.globalData.userInfo.Uid,
      success: function(res) {
         if(res.data.Code == 0){
          var NumberInfo = res.data.Data
          NumberInfo.typeNumber = NumberInfo.LineItem.Name + '(' + NumberInfo.LineItem.StartCode + NumberInfo.LineNumber + ')';//号码类型
          NumberInfo.myNumber = NumberInfo.LineItem.StartCode + NumberInfo.LineNumber;//您的号码
           that.setData({
             userNumberInfo: NumberInfo
           })
           wx.setNavigationBarTitle({
             title: "排队进度"
           })
         }
      },
    })
  },

  //获取店铺信息
  getStoreInfo:function(){
    var that = this;
    wx.request({
      url: app.globalData.host + "/api/Store/GetInfo?id=" + app.globalData.storeId,
      success: function(res) {
       if(res.data.Code == 0){
         var storeinfo = res.data.Data
         if (storeinfo.Logo){
           storeinfo.Logo = "https://static.711688.net.cn/upload/store/" + app.globalData.storeId + "/logo/source/" + storeinfo.Logo
         }
         that.setData({
           storeInfo: storeinfo
         })
       }
      },
    })
  },

  //选择就餐人数
  selectItem:function(e){
   var id = e.currentTarget.dataset.id;
   this.getFetchNumber(id);
   this.hideModal();
  },
   
  //客户取号
  getFetchNumber:function(id){
    var that =this;
    wx.request({
      url: app.globalData.host + "/api/Restaurant/FetchNumber?lineId=" + id+ "&uid=" + app.globalData.userInfo.Uid,
      success: function(res) {
        if(res.data.Code == 0){
          that.getNumberInfo();
          wx.showToast({
            title: '取号成功',
            icon: 'success',
            duration: 1000
          })
          that.setData({
            showInline: res.data.Data
          })
        }else{
          wx.showModal({
            title: '提示',
            content: res.data.Message,
            showCancel: false,
          })
        }
      },
    })
  },
  
  //取消排队
  cancelQueueNumber:function(){
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否确定取消排队',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.globalData.host + "/api/Restaurant/CancelQueue?storeId=" + app.globalData.storeId + "&uid=" + app.globalData.userInfo.Uid,
            success: function (res) {
              if (res.data.Code == 0) {
                wx.showToast({
                  title: '取消成功',
                  icon: 'success',
                  duration: 2000,
                  success:function(){
                    that.checkInline();
                  }
                })
              }
            },
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  //获取当前经纬度
  getLocationInfo:function(){
    var that = this;
    that.setData({
      showAnimation:false
    })
   wx.getLocation({
     type: 'wgs84',
     success: function (res) {
       var ltitude = res.latitude
       var lngitude = res.longitude
       that.getRegionByAdress(ltitude, lngitude)
     }
   })
  },

 //获取当前位置信息
 getRegionByAdress: function (ltitude, lngitude){
   var that = this;
   wx.request({
     url: app.globalData.host + "/api/Common/GetRegionByGeo?lat=" + ltitude + "&lng=" + lngitude,
     success: function(res) {
        if(res.data.Code == 0){
          var locationInfo = res.data.Data
          locationInfo.adressInfo = locationInfo.ProvinceName + locationInfo.CityName + locationInfo.Name;
          that.setData({
            locationAdress: locationInfo,
            showAnimation:true
          })
        }
     },
   })
 },

  // 弹出层
  showModal: function (e) {
    // 显示遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    this.setData({
      animationData: animation.export(),
      showModalStatus: true
    })
    setTimeout(function () {
      this.setData({
        animationData: animation.export()
      })
    }.bind(this), 200)
  },
  hideModal: function () {
    // 隐藏遮罩层
    var animation = wx.createAnimation({
      duration: 200,
      timingFunction: "linear",
      delay: 0
    })
    this.animation = animation
    this.setData({
      animationData: animation.export(),
    })
    setTimeout(function () {
      this.setData({
        animationData: animation.export(),
        showModalStatus: false
      })
    }.bind(this), 200)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.checkInline();
  }
})